﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConnectSQLServer.Controls;
using ConnectSQLServer.Common;
using ConnectSQLServer.Manager;
using Crownwood.Magic.Docking;
using Crownwood.Magic.Controls;

namespace ConnectSQLServer
{
    public partial class MSSQLServerExplorer : Form
    {
        public MSSQLServerExplorer()
        {
            InitializeComponent();
           
        }

        private void MSSQLServerExplorer_Load(object sender, EventArgs e)
        {

            this.splitContainer1.Panel1.Controls.Clear();
            this.splitContainer1.Panel2.Controls.Clear();

            GlobalVariable.QManager = new QueryManager();
            GlobalVariable.TManager = new TableManager();
            GlobalVariable.contextMenuEditor = this.contextMenuEditor;
            GlobalVariable.contextMenuDBS = this.contextMenuStripDBS;
            GlobalVariable.contextMenuDB = this.contextMenuStripDB;
            GlobalVariable.contextMenuTable = this.contextMenuTable;
            GlobalVariable.contextMenuStored = this.contextMenuStored;

            GlobalVariable.widthPanel2 = splitContainer1.Panel2.Size.Width;
            GlobalVariable.heightPanel2 = splitContainer1.Panel2.Size.Height;
            GlobalVariable.DBExplorerContainer = new ContainerControl();
            GlobalVariable.DBExplorerContainer.Size = new System.Drawing.Size(this.splitContainer1.Panel1.ClientSize.Width, this.splitContainer1.Panel1.ClientSize.Height);
            GlobalVariable.DBExplorerManager = new DatabaseExplorerManager(GlobalVariable.DBExplorerContainer);
            this.splitContainer1.Panel1.Controls.Add(GlobalVariable.DBExplorerContainer);

            //create tabcontrol;
            GlobalVariable.myTabControl = new MyTabControl();
            GlobalVariable.TCManager = new TabControlManager(GlobalVariable.myTabControl.MainTabControl);
            GlobalVariable.myTabControl.Size = new System.Drawing.Size(this.splitContainer1.Panel2.ClientSize.Width, this.splitContainer1.Panel2.ClientSize.Height);
            this.splitContainer1.Panel2.Controls.Add(GlobalVariable.myTabControl);
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            
            GlobalVariable.DBExplorerContainer.Size = new System.Drawing.Size(this.splitContainer1.Panel1.ClientSize.Width, this.splitContainer1.Panel1.ClientSize.Height);
            GlobalVariable.DBExplorerManager.CONTENT.DisplaySize = GlobalVariable.DBExplorerContainer.Size;
            GlobalVariable.DBExplorerManager.DOCKINGManager.HideContent(GlobalVariable.DBExplorerManager.CONTENT);
            GlobalVariable.DBExplorerManager.DOCKINGManager.ShowContent(GlobalVariable.DBExplorerManager.CONTENT);

            GlobalVariable.heightPanel2 = splitContainer1.Panel2.Height;
            GlobalVariable.widthPanel2 = splitContainer1.Panel2.Width;
            GlobalVariable.myTabControl.Width = splitContainer1.Panel2.Width;
            GlobalVariable.myTabControl.Size = new Size(splitContainer1.Panel2.Size.Width, splitContainer1.Panel2.Size.Height);
        }

        private void splitContainer1_SizeChanged(object sender, EventArgs e)
        {
            if (GlobalVariable.TCManager != null)
            {
                GlobalVariable.heightPanel2 = splitContainer1.Panel2.Height;
                GlobalVariable.widthPanel2 = splitContainer1.Panel2.Width;
                GlobalVariable.myTabControl.Size = new Size(splitContainer1.Panel2.Size.Width, splitContainer1.Panel2.Size.Height);
            }

        }
        #region Event handle

        //Evennt handle menu item
        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            GlobalVariable.QManager.NewQuery();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
           
            string pathname = string.Empty;
            string filename = string.Empty;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open file";
            ofd.Filter = "txt files (*.txt)|*.txt|sql files (*.sql)|*.sql";
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pathname = ofd.FileName;
                filename = ofd.SafeFileName;
            }

            if (string.IsNullOrEmpty(pathname) == false || string.IsNullOrEmpty(filename) == false)
                GlobalVariable.QManager.OpenFileQuery(pathname, filename);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string text = GlobalVariable.QManager.GetContentFromEditor();
            if (string.IsNullOrEmpty(text) == false)
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.DefaultExt = "*.txt|*.sql";
                saveFile.Filter = "TXT Files|*.txt|SQL Files|*.sql";

                if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
                   saveFile.FileName.Length > 0)
                {
                    System.IO.File.WriteAllText(saveFile.FileName, text);
                }
            }
        }

        private void toolStripExecute_Click(object sender, EventArgs e)
        {
            string text = Clipboard.GetText();
            Clipboard.Clear();
            GlobalVariable.QManager.Copy();
            string text1 = Clipboard.GetText();
            Clipboard.Clear();
            Clipboard.SetText(text);
            MessageBox.Show(text1);
        }

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            GlobalVariable.QManager.Cut();
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {
            GlobalVariable.QManager.Copy();
        }

        private void pasteToolStripButton_Click(object sender, EventArgs e)
        {
            GlobalVariable.QManager.Paste();
        }

        private void toolStripUndo_Click(object sender, EventArgs e)
        {
            GlobalVariable.QManager.Undo();
        }

        private void toolStripRedo_Click(object sender, EventArgs e)
        {
            GlobalVariable.QManager.Redo();
        }
        
        //Event handle context menu editor

        private void executeQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripExecute_Click(sender, e);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.saveToolStripButton_Click(sender, e);
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripUndo_Click(sender, e);
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.toolStripRedo_Click(sender, e);
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.cutToolStripButton_Click(sender, e);
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.copyToolStripButton_Click(sender, e);
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.pasteToolStripButton_Click(sender, e);
        }

        #endregion

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            GlobalVariable.TCManager.ShowResurlControl(GlobalVariable.TCManager.GetTabPageSelected());
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            GlobalVariable.TCManager.HideResurlControl(GlobalVariable.TCManager.GetTabPageSelected());
        }

    }
}
